<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class data_riwayat_pasien extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('breadcrumb');
		$this->load->model('m_pasien_data');
		$this->load->model('m_riwayat_pasien');
	}

	function index() {
		redirect("pasien_data", "refresh");
	}

	public function show($user_id=""){
		if($user_id != 0 && $user_id != ""){
			$this->load->library('paging');

			/* alergi */
			$data['alergi'] = $this->m_riwayat_pasien->get_alergi($user_id);
			/* riwayat penyakit */
			$data['r_penyakit'] = $this->m_riwayat_pasien->get_r_penyakit($user_id);
			/* riwayat obat */
			$data['r_obat'] = $this->m_riwayat_pasien->get_r_obat($user_id);
			$data['r_keluarga'] = $this->m_riwayat_pasien->get_r_keluarga($user_id);			
			$data['r_sosial'] = $this->m_riwayat_pasien->get_r_sosial($user_id);
			$data['r_kunjungan'] = $this->m_riwayat_pasien->get_r_kunjungan($user_id);
			$data['r_hasillab'] = $this->m_riwayat_pasien->get_hasillab($user_id);

			$judul = "Data Riwayat Pasien";
			$data['judul'] = $judul;
			$this->layout->set_title($judul);
			$this->layout->set_desc("lorem ipsum");
			$data['obat'] = $this->m_riwayat_pasien->get_obat();

			#breadcrumbs
			$this->breadcrumb->clear();
			$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
			$this->breadcrumb->add_crumb('Pasien', site_url('pasien_data'));
			$this->breadcrumb->add_crumb($judul);

			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');
			$this->layout->set_include('js', 'themes/apotek/js/olide-custom.js');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
			$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
			$this->layout->set_include('css', 'themes/general/bundle/select2/select2.min.css');
			$this->layout->set_include('css', 'themes/apotek/bundle/bootstrap3-editable/css/bootstrap-editable.css');
			$this->layout->set_include('js', 'themes/apotek/bundle/bootstrap3-editable/js/bootstrap-editable.js');
			$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
			$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
			$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
			$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

			$this->load->helper('trim_text');
			$data['pasien'] = $this->m_pasien_data->get_by_id($user_id);

			$this->layout->theme('apotek','data_riwayat_pasien/index', $data);
		}else{
			show_404();
		}
	}

	/* alergi section */
	public function alergi_add($user_id){
		if($user_id != 0 && $user_id != ""){
			if(isset($_POST)){
				$input = $this->input->post();
				$this->m_riwayat_pasien->insert_alergi($input, $user_id);
				
				redirect("data_riwayat_pasien/show/$user_id", 'refresh');
			}else{
				show_404();
			}
		}else{
			show_404();
		}
	}
	public function update_alergi(){
		$input  = $this->input->post('pk');
		$post_arr = explode(",", $input); 

		$KdAlergi = $post_arr[0];
		$KdPasien = $post_arr[1];
		$col = $this->input->post('name');

		if($col == "NmAlergi"){
			$dataUpdate = array(
				'Alergi' => $this->input->post('value'), 
				);
		}else if($col == "KetAlergi"){
			$dataUpdate = array(
				'Keterangan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_alergi($KdPasien, $KdAlergi, $dataUpdate);
	}
	public function alergi_destroy($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdAlergi', 'alergi')){
			set_flash('danger','Ups, Kelompok data alergi ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_riwayat_pasien->destroy_alergi($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	/* riwayat penyakit */
	public function r_penyakit_add($user_id){
		if($user_id != 0 && $user_id != ""){
			if(isset($_POST)){
				$input = $this->input->post();
				$this->m_riwayat_pasien->insert_r_penyakit($input, $user_id);
				
				redirect("data_riwayat_pasien/show/$user_id", 'refresh');
			}else{
				show_404();
			}
		}else{
			show_404();
		}
	}
	public function update_penyakit(){
		$input  = $this->input->post('pk');
		$post_arr = explode(",", $input); 

		$KdRPenyakit = $post_arr[0];
		$KdPasien = $post_arr[1];
		$col = $this->input->post('name');

		if($col == "NmPenyakit"){
			$dataUpdate = array(
				'Penyakit' => $this->input->post('value'), 
				);
		}else if($col == "KetPenyakit"){
			$dataUpdate = array(
				'Keterangan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_penyakit($KdPasien, $KdRPenyakit, $dataUpdate);
	}
	public function penyakit_destroy($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdRPenyakit', 'r_penyakit')){
			set_flash('danger','Ups, Data riwayat penyakit ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_riwayat_pasien->destroy_penyakit($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	/* riwayat obat */
	public function r_obat_add($user_id){
		if($user_id != 0 && $user_id != ""){
			if(isset($_POST)){
				$input = $this->input->post();
				$this->m_riwayat_pasien->insert_r_obat($input, $user_id);
				
				redirect("data_riwayat_pasien/show/$user_id", 'refresh');
			}else{
				show_404();
			}
		}else{
			show_404();
		}
	}
	public function update_r_obat(){
		$input  = $this->input->post('pk');
		$post_arr = explode(",", $input); 

		$KdRObat = $post_arr[0];
		$KdPasien = $post_arr[1];
		$col = $this->input->post('name');

		if($col == "Dosis"){
			$dataUpdate = array(
				'Dosis' => $this->input->post('value'), 
				);
		}else if($col == "Keterangan"){
			$dataUpdate = array(
				'Keterangan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_r_obat($KdPasien, $KdRObat, $dataUpdate);
	}
	public function r_obat_destroy($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdRObat', 'r_obat')){
			set_flash('danger','Ups, Data riwayat obat ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_riwayat_pasien->destroy_r_obat($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	/* riwayat keluarga */
	public function r_keluarga_add($user_id){
		if($user_id != 0 && $user_id != ""){
			if(isset($_POST)){
				$input = $this->input->post();
				$this->m_riwayat_pasien->insert_r_keluarga($input, $user_id);
				
				redirect("data_riwayat_pasien/show/$user_id", 'refresh');
			}else{
				show_404();
			}
		}else{
			show_404();
		}
	}
	public function update_keluarga(){
		$input  = $this->input->post('pk');
		$post_arr = explode(",", $input); 

		$KdKeluarga = $post_arr[0];
		$KdPasien = $post_arr[1];
		$col = $this->input->post('name');

		if($col == "NmKeluarga"){
			$dataUpdate = array(
				'Keluarga' => $this->input->post('value'), 
				);
		}else if($col == "KetKeluarga"){
			$dataUpdate = array(
				'Keterangan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_keluarga($KdPasien, $KdKeluarga, $dataUpdate);
	}
	public function keluarga_destroy($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdKeluarga', 'r_keluarga')){
			set_flash('danger','Ups, Data riwayat keluarga ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_riwayat_pasien->destroy_keluarga($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	/* riwayat sosial */
	public function r_sosial_add($user_id){
		if($user_id != 0 && $user_id != ""){
			if(isset($_POST)){
				$input = $this->input->post();
				$this->m_riwayat_pasien->insert_r_sosial($input, $user_id);
				
				redirect("data_riwayat_pasien/show/$user_id", 'refresh');
			}else{
				show_404();
			}
		}else{
			show_404();
		}
	}
	public function update_sosial(){
		$input  = $this->input->post('pk');
		$post_arr = explode(",", $input); 

		$KdRSosial = $post_arr[0];
		$KdPasien = $post_arr[1];
		$col = $this->input->post('name');

		if($col == "NmSosial"){
			$dataUpdate = array(
				'Sosial' => $this->input->post('value'), 
				);
		}else if($col == "KetSosial"){
			$dataUpdate = array(
				'Keterangan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_sosial($KdPasien, $KdRSosial, $dataUpdate);
	}
	public function sosial_destroy($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdRSosial', 'r_sosial')){
			set_flash('danger','Ups, data sosial ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_riwayat_pasien->destroy_sosial($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	/* kunjungan */
	public function update_kunjungan(){
		$input  = $this->input->post('pk');
		$post_arr = explode(",", $input); 

		$KdKunjungan = $post_arr[0];
		$KdPasien = $post_arr[1];
		$col = $this->input->post('name');

		if($col == "TglKunjungan"){
			$dataUpdate = array(
				'Penyakit' => $this->input->post('value'), 
				);
		}else if($col == "Keluhan"){
			$dataUpdate = array(
				'Keluhan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_kunjungan($KdPasien, $KdKunjungan, $dataUpdate);
	}
	public function kunjungan_destroy($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdKunjungan', 'kunjungan')){
			set_flash('danger','Ups, data kunjungan ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_riwayat_pasien->destroy_kunjungan($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	/* hasil lab dan resep */
	public function r_hasillab_add(){
		if(isset($_POST)){
			$input = $this->input->post();
			$this->m_riwayat_pasien->insert_r_hasillab($input);

			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}else{
			show_404();
		}
	}
	public function hasillab_kunjungan(){
		$input  = $this->input->post('pk');
		$post_arr = explode(",", $input); 

		$KdKunjungan = $post_arr[0];
		$KdPasien = $post_arr[1];
		$col = $this->input->post('name');

		if($col == "TglKunjungan"){
			$dataUpdate = array(
				'Penyakit' => $this->input->post('value'), 
				);
		}else if($col == "Keluhan"){
			$dataUpdate = array(
				'Keluhan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_kunjungan($KdPasien, $KdKunjungan, $dataUpdate);
	}
	public function hasillab_destroy($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdHasilLab', 'r_hasil_lab')){
			set_flash('danger','Ups, data hasil lab tidak ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_riwayat_pasien->destroy_hasillab($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}
	public function update_hasillab(){
		$KdHasilLab = $this->input->post('pk');
		$col = $this->input->post('name');

		if($col == "Hasil"){
			$dataUpdate = array(
				'Hasil' => $this->input->post('value'), 
				);
		}else if($col == "Keluhan"){
			$dataUpdate = array(
				'Keluhan' => $this->input->post('value'), 
				);
		}

		$this->m_riwayat_pasien->update_hasillab($KdHasilLab, $dataUpdate);
	}
}

